package nio.channel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ChannelTest1 {

	public static void main(String[] args) throws IOException {
		
		
		FileInputStream fis = new FileInputStream( "沧海一声笑.mp3" );
		// 通过 文件输入流 来获取 FileChannel
		FileChannel first = fis.getChannel();
		System.out.println( first );
		first.close();
		fis.close();
		
		System.out.println( "- - - - - - - - - - - - - - - - - - - - - - - -" );
		
		FileOutputStream fos = new FileOutputStream( "file-output-channel.txt" );
		// 通过 文件输出流 来获取 FileChannel
		FileChannel second = fos.getChannel();
		System.out.println( second );
		second.close();
		fos.close();
		
		System.out.println( "- - - - - - - - - - - - - - - - - - - - - - - -" );
		
		RandomAccessFile raf = new RandomAccessFile( "random-access-channel.txt" , "rw" );
		
		FileChannel third = fos.getChannel();
		System.out.println( third );
		
		third.close();
		raf.close();
		
		System.out.println( "= = = = = = = = = = = = = = = = = = = = = = = = =" );
		
		// 从 JDK 1.7 开始
		
		Path path = Paths.get( "src" , "nio" , "channel" , "ChannelTest1.java" ); // JDK 1.7
		// Path path = Path.of("src" , "nio" , "channel" , "ChannelTest1.java" ); // JDK 11
		
		FileChannel fourth = FileChannel.open( path ); // JDK 1.7
		System.out.println( fourth );
		fourth.close();
		
		System.out.println( "- - - - - - - - - - - - - - - - - - - - - - - -" );
		
		SeekableByteChannel channel = Files.newByteChannel( path );
		System.out.println( channel );
		channel.close();

	}

}
